package isp.apps.example.shoppingadmin.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.domain.ShoppingCartValidator;
import isp.apps.example.shoppingcart.domain.ShippingOption;
import isp.apps.example.shoppingcart.domain.ShoppingCartMessageFactory;
import isp.apps.example.shoppingcart.persistence.ShippingOptionsRepository;
import isp.apps.example.shoppingcart.persistence.factory.ShippingOptionsPersistenceFactory;
import isp.apps.messageFramework.SystemMessage;
import isp.apps.messageFramework.retriever.MessageRetriever;

import java.math.BigDecimal;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NewShippingOptionAction extends ActionSupport
{
  private MessageRetriever messageRetriever = ShoppingCartMessageFactory.getMessageRetriever();
  private String newShippingOptionServiceName, newShippingOptionCompanyName, newShippingOptionTime, newShippingOptionRate;
  public static final Pattern SEVEN_TWO_DECIMAL = Pattern.compile("[\\d]{1,7}+([\\.][\\d]{2}+)??");
  public static final Pattern NON_ALPHANUMERIC = Pattern.compile("\\W");
  public static final Pattern NON_DIGIT = Pattern.compile("\\D");

  public NewShippingOptionAction()
  {
  }
     
  public String execute()
  {
    validate();
    if (hasActionErrors())
      return SUCCESS;
    writeToRepository(); 
    return SUCCESS;
  }

  public void validate()
  {
    //Matcher m;
    ShoppingCartValidator scv = new ShoppingCartValidator();
    
    if ((newShippingOptionCompanyName = scv.checkText(newShippingOptionCompanyName)) == null)
    //if (newShippingOptionCompanyName.equals(""))
    {
      addActionError("Please specify a valid company name for the new shipping option.");
      return;
    }
    
    if ((newShippingOptionServiceName = scv.checkText(newShippingOptionServiceName)) == null)
    //if (newShippingOptionServiceName.equals(""))
    {
      addActionError("Please enter a valid service name for the new shipping option");
      return;
    }

    //m = SEVEN_TWO_DECIMAL.matcher(newShippingOptionRate);
    //if (!m.matches())
    if ((newShippingOptionRate = scv.checkCurrency(newShippingOptionRate)) == null)
    {
      addActionError("Please specify a valid rate for the new shipping option (1-7 digits, optionally 2 decimal places).");
      return;
    }
    
    //m = NON_DIGIT.matcher(newShippingOptionTime);
    //if (m.find())
    if ((newShippingOptionTime = scv.checkNumber(newShippingOptionTime)) == null)
    {
      addActionError("Please enter a valid time (in days) for the new shipping option.");
      return;
    }
  }

  public void writeToRepository()
  {
    ShippingOptionsRepository sor = ShippingOptionsPersistenceFactory.getShippingOptionsRepository();
    ShippingOption so = new ShippingOption();
    so.setCompanyName(newShippingOptionCompanyName);
    so.setRate(new BigDecimal(newShippingOptionRate));
    so.setServiceName(newShippingOptionServiceName);
    so.setTime(new Integer(newShippingOptionTime));
    so.setValid(1);
    sor.saveShippingOption(so);
  }

  public void setNewShippingOptionServiceName(String newShippingOptionServiceName)
  {
    this.newShippingOptionServiceName = newShippingOptionServiceName.trim();
  }

  public String getNewShippingOptionServiceName()
  {
    return newShippingOptionServiceName;
  }

  public void setNewShippingOptionCompanyName(String newShippingOptionCompanyName)
  {
    this.newShippingOptionCompanyName = newShippingOptionCompanyName.trim();
  }

  public String getNewShippingOptionCompanyName()
  {
    return newShippingOptionCompanyName;
  }

  public void setNewShippingOptionTime(String newShippingOptionTime)
  {
    this.newShippingOptionTime = newShippingOptionTime.trim();
  }

  public String getNewShippingOptionTime()
  {
    return newShippingOptionTime;
  }

  public void setNewShippingOptionRate(String newShippingOptionRate)
  {
    this.newShippingOptionRate = newShippingOptionRate.trim();
  }

  public String getNewShippingOptionRate()
  {
    return newShippingOptionRate;
  }
}
